import axios from 'axios'
import store from '@/store'
import notification from 'ant-design-vue/es/notification'
import { VueAxios } from './axios'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import {paramsSign} from "./util"
// import { ADMIN_PLATFORM_CODE, ADMIN_PLATFORM_NAME } from './index.js'
import storage from 'store'
import router from '@/router'

// 创建 axios 实例
const service = axios.create({
  baseURL: process.env.VUE_APP_API_BASE_URL, // api base_url
  timeout: 5 * 60 * 1000, // 请求超时时间，
  headers: {
    'Content-Type': 'application/json'
  }
})

const err = (error) => {
  if (error.response) {
    const data = error.response.data
    const token = storage.get(ACCESS_TOKEN)
    if (error.response.status === 403) {
      notification.error({
        message: 'Forbidden',
        description: data.msg
      })
      console.log(token, 'token')
      if (token) {
        store.dispatch('Logout').then(() => {
          setTimeout(() => {
            window.location.reload()
          }, 1500)
        })
      }
    }
    if (
      error.response.status === 401 &&
      !(data.result && data.result.isLogin)
    ) {
      notification.error({
        message: 'Unauthorized',
        description: 'Authorization verification failed'
      })
      if (token) {
        store.dispatch('Logout').then(() => {
          setTimeout(() => {
            window.location.reload()
          }, 1500)
        })
      }
    }
  }
  return Promise.reject(error)
}

// request interceptor
service.interceptors.request.use((config) => {
  const token = storage.get(ACCESS_TOKEN)

  console.log(config, '----config.data')
  if(config.data && config.data.pageSize){
    config.data.current = config.data.pageNum
    config.data.pageNo = config.data.pageNum
  }
  if (token) {
    config.headers['token'] = token // 让每个请求携带自定义 token 请根据实际情况自行修改
  }
  // console.log(config, '----config.data2')
  // paramsSign(config)
  return config
}, err)

// response interceptor
service.interceptors.response.use((response) => {
  // return response.data
  if (response.data instanceof Blob) {
    const disposition = response.headers['content-disposition']
    console.log(disposition, response, 'disposition')
    // if (disposition !== undefined) {
    //   const fileName = disposition.split(';')[1].split('=')[1]
    //   return {
    //     data: response.data,
    //     fileName: decodeURIComponent(fileName)
    //   }
    // }
    return {
      data: response.data
    }
  } else {
    const { code, msg } = response.data
    console.log(response, 'response.data')
    if (code == 403) {
      notification['error']({
        message: '授权过期',
        description: '请重新登录'
      })
      store.dispatch('Logout').then(() => {
        window.location.reload()
        router.push({ name: 'Login' })
      })
    } else if (code !== "0000") {
      notification['error']({
        message: '错误提示',
        description: msg || '未知错误'
      })
      return Promise.reject(new Error(msg))
    }
    return response.data
  }
}, err)

const installer = {
  vm: {},
  install (Vue) {
    Vue.use(VueAxios, service)
  }
}

export { installer as VueAxios, service as axios }
